/**
 * 获取输入字数统计
 * @param str
 * @param maxLegnth
 * @returns {string}
 */
const getInputLength = (str, maxLegnth) => {
  return `${(str || '').length} / ${maxLegnth}`
}

/**
 * 判断指定字符串是否http url
 * @param str
 * @returns {boolean}
 */
const isHttpUrl = (str) => {
  const reg = new RegExp('(http|https)://[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9-._?,\'/\\+&$%$#=~])*$')
  return reg.test(str)
}

/**
 * 判断是否手机号
 * @param str
 */
const isPhone = (str) => {
  return /^1[3456789]\d{9}$/g.test(str)
}

let seedId = Math.floor(Math.random() * 10000000)
/**
 * 获取自增id
 * @returns {number}
 */
const getIncrementId = () => {
  seedId++
  return seedId
}

// 深克隆
const deepCloneData = (o) => {
  // 判断如果不是引用类型，直接返回数据即可
  if (typeof o === 'string' || typeof o === 'number' || typeof o === 'boolean' || typeof o === 'undefined') {
    return o
  } else if (Array.isArray(o)) {
    // 如果是数组，则定义一个新数组，完成复制后返回
    // 注意，这里判断数组不能用typeof，因为typeof Array 返回的是object
    console.log(typeof [])  // --> object
    var _arr = []
    o.forEach(item => { _arr.push(item) })
    return _arr
  } else if (typeof o === 'object') {
    var _o = {}
    for (let key in o) {
        _o[key] = deepCloneData(o[key])
    }
    return _o
  }

  // if (!data || !(data instanceof Object) || (typeof data === 'function')) {
  //   return data || undefined
  // }
  // var constructor = data.constructor
  // var result = new constructor()
  // for (var key in data) {
  //   if (data.hasOwnProperty(key)) {
  //     result[key] = deepCloneData(data[key])
  //   }
  // }
  // return result
}

// 获取设备 pc true : app false
const sUserAgent = navigator.userAgent.toLowerCase()
export const getPlatform = () => {
  const bIsIpad = sUserAgent.match(/ipad/i) == 'ipad'
  const bIsIphoneOs = sUserAgent.match(/iphone os/i) == 'iphone os'
  const bIsMidp = sUserAgent.match(/midp/i) == 'midp'
  const bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == 'rv:1.2.3.4'
  const bIsUc = sUserAgent.match(/ucweb/i) == 'ucweb'
  const bIsAndroid = sUserAgent.match(/android/i) == 'android'
  const bIsCE = sUserAgent.match(/windows ce/i) == 'windows ce'
  const bIsWM = sUserAgent.match(/windows mobile/i) == 'windows mobile'

  if (
    bIsIpad ||
    bIsIphoneOs ||
    bIsMidp ||
    bIsUc7 ||
    bIsUc ||
    bIsAndroid ||
    bIsCE ||
    bIsWM
  ) {
    return false
  } else {
    return true
  }
}

export default { getInputLength, isHttpUrl, getIncrementId, isPhone, deepCloneData, getPlatform }
